#include "CFileExporter.h"
#include <stdlib.h>

CFileExporter::CFileExporter(std::string sAudioFileName, std::string sVideoFileName)
{	
	m_pAudioFile = fopen (sAudioFileName.c_str(), "wb");
	if (m_pAudioFile == NULL)
	{
		perror ("Error opening output file");
		exit(-1);
	}
	m_pVideoFile = fopen (sVideoFileName.c_str(), "wb");
	if (m_pVideoFile == NULL)
	{
		perror ("Error opening output file");
		exit(-1);
	}
}

CFileExporter::~CFileExporter(void)
{
	fclose(m_pAudioFile);
	fclose(m_pVideoFile);
}


void CFileExporter::outputAUs(IAUBuilder* cAUBuilder)
{
	_writeAus(cAUBuilder->m_audioAus, m_pAudioFile);
	_writeAus(cAUBuilder->m_videoAus, m_pVideoFile);
}


void CFileExporter::_writeAus(std::vector<CDynamicArray*>& aus, FILE* pFile)
{
	CDynamicArray* sCurrentAU;
	for(unsigned int i=0; i<aus.size(); i++)
	{
		sCurrentAU = aus.at(i);
		if (sCurrentAU->m_nSize != fwrite(sCurrentAU->m_cArray, 1 ,sCurrentAU->m_nSize, pFile))
		{
			perror ("Error writing to output file");
			exit(-1);
		}
		delete sCurrentAU;
	}
	aus.clear();
}
